package com.nhutcm.trackingthief;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.nhutcm.trackingthief.service.TrackingThiefService;
import com.nhutcm.trackingthief.utils.Constant;
import com.nhutcm.trackingthief.utils.PrefUtils;
import com.nhutcm.trackingthief.utils.ReceiverUtils;

public class RegisterScreen extends Activity {

	private EditText txtEmail;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.layout_register);

		txtEmail = (EditText) findViewById(R.id.txtEmail);

		Button btnRegister = (Button) findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (txtEmail.getText().toString().equals("")) {
					return;
				}

				/** Show dialog to confirm email */
				confirmEmail();

			}
		});

		txtEmail.postDelayed(new Runnable() {
			@Override
			public void run() {
				showSoftKeyboard(RegisterScreen.this, txtEmail);
			}
		}, 1000);

	}

	public void showSoftKeyboard(Context context, View view) {
		if (view.requestFocus()) {
			InputMethodManager imm = (InputMethodManager) context
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
		}
	}

	private void confirmEmail() {

		AlertDialog.Builder alertDialog = new AlertDialog.Builder(
				RegisterScreen.this);
		alertDialog.setMessage(getString(R.string.are_you_sure));
		alertDialog.setPositiveButton(getString(R.string.yes),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String email = txtEmail.getText().toString();
						if (email.equals("")) {
							Toast.makeText(RegisterScreen.this,
									getString(R.string.plese_enter_your_email),
									Toast.LENGTH_SHORT).show();
							return;
						}
						saveDefaultConfig(email);
						
						confirmRunAppInBackground();
						
					}

					private void saveDefaultConfig(String email) {

						/** Saving email */
						saveEmailAddress(email);

						/** Saving serialnumber */
						saveSimSerialNumber();

						/** Saving phone state */
						savePhoneState();

						/** Saving fileDirs path */
						saveFileDirsPath(RegisterScreen.this.getFilesDir()
								.getPath());
					}

				});
		alertDialog.setNegativeButton(getString(R.string.no),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});

		alertDialog.show();

	}

	@SuppressWarnings("deprecation")
	private void confirmRunAppInBackground() {
		AlertDialog alertDialog = new AlertDialog.Builder(RegisterScreen.this)
				.create();
		alertDialog.setMessage(getString(R.string.app_will_be_hiden));
		alertDialog.setCancelable(false);
		alertDialog.setButton(getString(R.string.ok),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						startService(new Intent(RegisterScreen.this,
								TrackingThiefService.class));

						ReceiverUtils.registerSecretReceiver(
								getApplicationContext(),
								Constant.DEDAULT_SECRECT_CODE);
						
						/* Hide App */
						hideApp(RegisterScreen.this);

						RegisterScreen.this.finish();
					}
				});
		alertDialog.show();
	}

	private void saveEmailAddress(String email) {
		PrefUtils.saveStringToPreference(RegisterScreen.this,
				getString(R.string.pref_email_key), email);
	}

	private void saveSimSerialNumber() {
		TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String simSerialNumber = telemamanger.getSimSerialNumber();
		PrefUtils.saveStringToPreference(RegisterScreen.this,
				getString(R.string.pref_phone_serial_number), simSerialNumber);

	}

	private void savePhoneState() {
		 PrefUtils.saveStringToPreference(getApplicationContext(),
				 getString(R.string.phone_state), "1");
	}

	private void saveFileDirsPath(String path) {
		PrefUtils.saveStringToPreference(RegisterScreen.this,
				getString(R.string.file_path), path);
	}

	void hideApp(Context context) {
		PackageManager p = getPackageManager();
		ComponentName componentName = new ComponentName(context,
				com.nhutcm.trackingthief.HomeScreen.class);
		p.setComponentEnabledSetting(componentName,
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
				PackageManager.DONT_KILL_APP);

		SharedPreferences share = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		share.edit().putBoolean(getString(R.string.pref_hide_app_key), true)
				.commit();

	}

	boolean doubleBackToExitPressedOnce = false;

	@Override
	public void onBackPressed() {
		if (doubleBackToExitPressedOnce) {
			super.onBackPressed();
			return;
		}

		this.doubleBackToExitPressedOnce = true;
		Toast.makeText(this, getString(R.string.exit), Toast.LENGTH_SHORT)
				.show();

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				doubleBackToExitPressedOnce = false;
			}
		}, 2000);
	}
}
